#include <stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        __int64 a,yu,b;
        scanf("%I64d%I64d",&yu,&b);
        for (a=b;;a+=b)
        {
            if (a%9973==yu)
            {
                printf("%I64d\n",(a/b)%9973);
                break;
            }
        }
    }
    return 0;
}
